import { _decorator, Node, Component, Animation, sp, sys, log, Toggle, Sprite, Label, Button } from 'cc';
import { UIManager } from '../../framework/manager/uiManager';
import { Constant } from '../../Enum/Enum';
import ClientContext from '../../gameCore/global/ClientContext';
import { NetManagerState } from '../../Enum/NetEnums';
import LogicBoot from '../../gameCore/logic/LogicBoot';
import { ClientEvent } from '../../framework/bo/ClientEvent';
import ConfigGame from '../../gameCore/global/ConfigGame';
import ClientManager from '../../gameCore/global/ClientManager';
import StorageMgr from '../../framework/manager/storageManager';
const { ccclass, property } = _decorator;

@ccclass('StartPanel')
export class StartPanel extends Component {

    // @property(sp.Skeleton)
    // startSk: sp.Skeleton = null;

    @property(Node)
    btn_start: Node = null!;

    @property(Label)
    LabelChannelInfo: Label = null!;

    @property(Animation)
    loadAni: Animation = null;

    @property(Node)
    btn_register: Node = null!;// 打开注册面板按钮

    @property(Node)
    btn_quit: Node = null!;//注销登录按钮，删除玩家客户端所有的userInfo

    @property(Node)
    toggle_lable: Node = null!;
    @property(Toggle)
    toggle_check: Toggle = null!;// 同意玩游戏选择点
    // 服务器信息展示
    @property(Node)
    serverInfo: Node = null!;
    @property(Button)
    btn_open_hotUpdate: Button = null;
    @property(Label)
    lable_version: Label = null;
    private _isClick: boolean = false;

    show() {
        // this.loadAni.node.active = false;
        // this.loadAni.stop();
        // this.startSk.setAnimation(0, 'start', false);
        // this.startSk.addAnimation(0, 'idle', true);// 骨骼动画中本来包含的状态
        this.btn_start.active = false;
        // this.btnNodeHw.active = false;
        // UIManager.instance.hideDialog(Constant.PANEL_NAME.LOADING01);
        this.btn_start.active = true;
        this.lable_version.string = `版本号：${ConfigGame.GAME_VERSION}`;
        this.LabelChannelInfo.string = `      代码中:
        channnel_id= ${ConfigGame.Channel_id}
        channelType= ${ConfigGame.Channel_type}
        缓存中:
        channnel_id=${StorageMgr.get("channel_id",false)}
        channelType=${StorageMgr.get("channel_type",false)}`;
    }

    protected onEnable(): void {
        ClientEvent.on(Constant.EVENT_NAME.on_init_server_info, this.setAreaInfoLable, this);
    }

    protected onDisable(): void {
        ClientEvent.off(Constant.EVENT_NAME.on_init_server_info, this.setAreaInfoLable, this);
    }

    private _initSuccess(code: number, msg: string) {
        // 展示区服信息

        // 账号登录 
    }

    /**
     * 开始游戏
     * 
     * @returns 
     */
    public async onStartGame() {
        // if (this._isClick) {
        //     return;
        // }
        // this._isClick = true;

        // this.loadAni.node.active = true;
        // this.loadAni.play();
        await this._loginGame();
        // ResourceUtil.releaseRes("prefab/ui/" + Constant.PANEL_NAME.START_GAME, null);
    }

    /**
     * 登录游戏服务器 服务器待开发哦 以后写
     */
    private async _loginGame() {
        // 登录
        if (ClientContext.Instance.hallSocketManager.stateNet == NetManagerState.disconnected) {
            LogicBoot.Instance.connect();
        } else {
            const instance = ClientContext.Instance;
            const res = instance.checkEnterGame(true);// 检测登陆，没有登陆则跳出登陆面板
            if (res == 0) {
                ClientManager.Instance.loadingHall();
            } else {
                UIManager.instance.showTips(res);
            }
        }
    }

    /**同意玩游戏 z=暂未使用*/
    public agreeToggle() {
        this.toggle_check.isChecked = !this.toggle_check.isChecked;
    }

    private setAreaInfoLable() {
        console.log("#setAreaInfo");
        // this.serverInfo.getChildByName
        let label: Label = this.serverInfo.getComponentInChildren(Label) as Label;
        const id = ClientContext.Instance.ServerInfos[0].areaInfos[0].areaId;
        const name = ClientContext.Instance.ServerInfos[0].areaInfos[0].areaName;
        const val = `区服:${id} 区名:${name}`;
        console.log(val);
        label.string = val;
    }

    public async quitLoginBtntargert() {
        new Promise<void>((resolve,) => {
            ClientContext.Instance.clearnUserInfo(() => {
                UIManager.instance.showTips(Constant.Tips.TIPS_5);
                resolve();
            });
        });
    }

    public registerBtntarget() {// 打开注册面板功能触发按钮
        UIManager.instance.showDialog(Constant.PANEL_NAME.LOGIN_PANEL);
    }

    public Btn_open_hotUpdatePanel() {// 打开热更新面板
        UIManager.instance.showDialog(Constant.PANEL_NAME.HOT_UPDATE_PANEL);
    }

    btn_gamerestart() {// 重启游戏按钮触发
        ClientManager.Instance.gameRestart(false);
    }

}

